/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import flash.external.ExternalInterface;
	/**
	 * 模板工作流中的输入组件 
	 * 
	 */	
	public class EDITemplateHTMLViewer extends EDIHTMLViewer
	{
		private var _useword:Boolean=false;
		private var ifRame:String="";
		public var documentStr:String;
		private var _fckeditValue:String="";
		private var _title:String="";
		   
		private static var FUNCTION_GETVALUE:String = 
            "document.insertScript = function ()" +
        	"{ " + 
                "if (document.getValue==null)" + 
                "{" +
                    "getValue = function (frameID,useword)" +
                	"{ " +
                		"try{" + 
                        "var divValue1=document.getElementById('_div_'+frameID).innerHTML;" + 
                        "var divValue=\"<template><![CDATA[\"+divValue1+\"]]></template>\";" +                        
                        "var htmlValue='';" + 
                        "if(useword=='1'){" + 
                        	"var oEditor = window.frames['_iframe_'+frameID].FCKeditorAPI.GetInstance(\"fck\"); " +
                        	"var htmlValue1 = oEditor.GetXHTML(true);" + 
                        	"htmlValue = \"<filecontent><![CDATA[\"+htmlValue1+\"]]></filecontent>\";" +	 
                        "}" + 
                        "return divValue+htmlValue;" +
                        "}catch(err){}"+
                    "}" +
                "}" +
            "}";
		
		private static var FUNCTION_SETVALUE:String = 
				"document.insertScript = function ()" +
	        	"{ " + 
	                "if (document.setTextToEditor==null)" + 
	                "{" + 
	                    "setTextToEditor = function(frameID,value)" +
	                	"{ " + 
	                		"try{" + 
	   	                	"document.all.fck.value=value;" + 
	                		"var oEditor = window.frames['_iframe_'+frameID].FCKeditorAPI.GetInstance(\"fck\"); " +
	                        "oEditor.SetHTML(value);"+
	                       	"}catch(err){}"+ 
	                    "}" +
	                "}" +
	            "}";
	            
	     private static var FUNCTION_GETTXTTITLE:String=
	     	"document.insertScript = function ()" + 
	     	"{" + 
	     		"if(document.getTxtTitle==null)" + 
	     		"{" + 
	     			"getTxtTitle=function()" + 
	     			"{" + 
	     				"try{"+
	     				"return document.getElementById('txtTitle').value;" + 
	     				 "}catch(err){}"+
	     			"}" + 
	     		"}" + 
	     	"}";  
	     	
	    private static var FUNCTION_SETTXTTITLE:String=
	     	"document.insertScript = function ()" + 
	     	"{" + 
	     		"if(document.setTxtTitle==null)" + 
	     		"{" + 
	     			"setTxtTitle=function(value)" + 
	     			"{" + 
	     				"try{"+
	     				"document.getElementById('txtTitle').value=value;" + 
	     				 "}catch(err){}"+
	     			"}" + 
	     		"}" + 
	     	"}";    	  
		
		private static var FUNCTION_CHOOSEUSER:String = " function selectsignalUser(destSelect){ "
	   +" var a=document.getElementById('sessionId').value; "
      +" var str=window.showModalDialog('/console/unit.do?op=selectUsersFrame&ediSessionId='+a,null,'status:no;resizable:yes;dialogHeight:400px;dialogWidth:600px;unadorne:yes'); "
    +"  if(str!=''&&!(typeof(str) == 'undefined')){    "    
      +"   var length=str.length; "
      +"   var  len = str.lastIndexOf(';'); "
		+" var ids=str.substring(0,len); "
		+" var names=str.substring(len+1,length); "
		+" var arryIds=ids.split(','); "
	+" 	var arryNames=names.split(','); "
		+" document.getElementsByName(destSelect)[0].value=  arryNames[0]; "
     +"  }"
 +" }";
 		private static var FUNCTION_CHOOSEUNIT:String = " function openUnitTree(destSelect){ "
	+" var a=document.getElementById('sessionId').value; "
    +"  var str=window.showModalDialog('/console/unit.do?op=getUnitTree&isSelect=true&ediSessionId='+a,null,'status:no;resizable:yes;dialogHeight:300px;dialogWidth:400px;unadorne:yes'); "
    +" if(str!=''&&!(typeof(str) == 'undefined')){     "   
     +"   var length=str.length; "
     +"   var  len = str.lastIndexOf('_'); "
	+"	document.getElementsByName(destSelect)[0].value =  str.substring(len+1,length); "
     +" }"

 +"}" ;
 		private static var FUNCTION_SELECTDATE:String = "function selectDate(ctrlobj){"+
		"showx = event.screenX - event.offsetX - 4 - 210 ; "+
        "showy = event.screenY - event.offsetY + 18;"+
        "newWINwidth = 210 + 4 + 18;"+
        "retval = window.showModalDialog('/app/"+EDIApplication.appName+"/jsp/common/calendar/calendardlg.htm?returnType=two', '', 'dialogWidth:197px; dialogHeight:210px; dialogLeft:'+showx+'px; dialogTop:'+showy+'px; status:no; directories:yes;scrollbars:no;Resizable=no; '  );"+
        "if( retval != null ){"+
            "ctrlobj.value = retval;"+
        "}"+
	"}";
		
		/**
		 * 构造  EDITemplateHTMLViewer
		 * 
		 */		
		public function EDITemplateHTMLViewer()
		{
			super();
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			ExternalInterface.call(FUNCTION_GETVALUE);
			ExternalInterface.call(FUNCTION_SETVALUE);
			ExternalInterface.call(FUNCTION_GETTXTTITLE);
			ExternalInterface.call(FUNCTION_SETTXTTITLE);
			ExternalInterface.call(FUNCTION_CHOOSEUSER);
			ExternalInterface.call(FUNCTION_CHOOSEUNIT);
			ExternalInterface.call(FUNCTION_SELECTDATE);
		}
		
		/**
		 * 使用正文 
		 * @param value
		 * 
		 */		
		public function set useword(value:Boolean):void
		{
			if(value)
			{
				ifRame="<tr><td><iframe id='_iframe_"+name+"' src='/frame/FCKEditor.jsp' frameborder='0' width='100%' height='400'></iframe></td></tr>";
			}
			_useword=value;
		}
		
		public function get useword():Boolean
		{
			return _useword;
		}
		
		/**
		 * 设置正文内容 
		 * @param value
		 * 
		 */		
		public function set FCKEDITValue(value:String):void
		{
			_fckeditValue=value;
			ExternalInterface.call("setTextToEditor",frameId,_fckeditValue);
		}
		
		/**
		 * 生成模板内容 
		 * 
		 */		
		public function build():void
		{
			var html:String = "<table width=100% ><tr width=100%><td align=center><br><strong><font style=font-size:14px>公文标题：</font></strong><input type=text id=txtTitle name=txtTitle size=70/><br><br><hr width=75%><br><br></td></tr><tr width=100% height=100% ><td id='_div_"+name+"' valign='top'>"+documentStr+"</td></tr>"+ifRame+"</table>";
			HTML=html;
			EDIApplication.outputDebug("-------------输出内容为：-----------------");
			EDIApplication.outputDebug(html);

			if(_fckeditValue!="")
			{
				ExternalInterface.call("setTextToEditor",frameId,_fckeditValue);
			}
			if(_title!="")
			{
				ExternalInterface.call("setTxtTitle",_title);
			}
		}
		
		/**
		 * 设置公文标题 
		 * @param value
		 * 
		 */		
		public function set title(value:String):void
		{
			_title=value;
			ExternalInterface.call("setTxtTitle",_title);
		}
		
		public function get title():String{
			_title=ExternalInterface.call("getTxtTitle");
			return _title
		}
		
		
		/**
		 * 获得模板内容 
		 * @return 
		 * 
		 */		
		override public function get HTML():String
		{
			return ExternalInterface.call("getValue",frameId,(useword?"1":"0"));
		}
		
	}
}